package com.huanzhidadi.singleweb.delayqueue;

import lombok.Data;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

@Data
public class DelayedUser implements Delayed {

    private String name;

    private long avaibleTime;

    public DelayedUser(String name, long delayTime) {
        this.name = name;
        this.avaibleTime = delayTime + System.currentTimeMillis();
    }

    @Override
    public long getDelay(TimeUnit unit) {
        // 判断avaibleTime是否大于当前系统时间
        long diffTime = avaibleTime - System.currentTimeMillis();
        return unit.convert(diffTime, TimeUnit.MILLISECONDS);
    }

    @Override
    public int compareTo(Delayed o) {
        return (int) (this.avaibleTime - ((DelayedUser) o).getAvaibleTime());
    }
}
